/* * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package me.angrybyte.numberpicker.demo;

import me.angrybyte.numberpicker.listener.OnValueChangeListener;
import me.angrybyte.numberpicker.utils.ResUtil;
import me.angrybyte.numberpicker.view.ActualNumberPicker;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.ComponentContainer;
import ohos.agp.utils.Color;

public class DemoAbility extends Ability implements OnValueChangeListener {

    private ActualNumberPicker mTestPicker;
    private ComponentContainer mContentRoot;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_demo);

        mTestPicker = (ActualNumberPicker) findComponentById(ResourceTable.Id_actual_picker2);
        mContentRoot = (ComponentContainer) findComponentById(ResourceTable.Id_content);
        mTestPicker.setListener(this);
    }

    private void updateBackgroundColor(float percent) {
        int[] colors = new int[] {
                Color.WHITE.getValue(), Color.GRAY.getValue(), Color.DKGRAY.getValue()
        };
        int color = colors[((int) Math.floor(percent * (colors.length - 1)))];

        mContentRoot.setBackground(ResUtil.buildDrawableByColor(color));
        mContentRoot.getComponentAt(0).setBackground(ResUtil.buildDrawableByColor(color));
    }

    @Override
    public void onValueChanged(int oldValue, int newValue) {
        float percent = (float) newValue / (float) (mTestPicker.getMaxValue() - mTestPicker.getMinValue());
        updateBackgroundColor(percent);
    }

}
